package com.ciwong.epaper.widget;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;

import com.bumptech.glide.Glide;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.msg.bean.WorkContent;
import com.ciwong.epaper.util.DisplayUtils;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;


/**
 * Created by keepon on 2018/8/7.
 */
public class DubVideoExpansionItemView extends BaseDubVideoItemInView<WorkContent> {

    public DubVideoExpansionItemView(Context context) {
        super(context);
    }

    public void bindData(WorkContent workContent) {
        try {
            if (!TextUtils.isEmpty(workContent.getCover())) {
                Uri uri = Uri.parse(workContent.getCover());
                setNetImg(mIvIcon, uri, 200, 100);
            } else {
                setLocalDefaultImg(mIvIcon);
            }
        } catch (Exception e) {

        } catch (OutOfMemoryError error) {

        }
        mTvName.setText(workContent.getResourceName());
    }

    private void setNetImg(SimpleDraweeView simpleDraweeView, Uri uri, int width, int height) {
        try {
            ImageRequest request = null;
            if (uri != null) {
                request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(DisplayUtils.dip2px(simpleDraweeView.getContext(), width), DisplayUtils.dip2px(simpleDraweeView.getContext(), height))).build();

                DraweeController drawweeController = Fresco.newDraweeControllerBuilder().setUri(uri).setImageRequest(request)
                        //列表滑动会变流畅
                        .setOldController(simpleDraweeView.getController())
                        //                    .setAutoPlayAnimations(true)
                        .build();
                simpleDraweeView.setController(drawweeController);
            }
        } catch (Exception e) {

        } catch (OutOfMemoryError outOfMemoryError) {

        }


    }

    private void setLocalDefaultImg(SimpleDraweeView simpleDraweeView) {
        Uri uri = Uri.parse("res:///" + R.mipmap.item_dub_video_default);
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setTapToRetryEnabled(true)
                .build();
        simpleDraweeView.setController(controller);
    }
}
